package com.bmadmin.service.impl;

import com.bmadmin.domain.DataSourcePOJO;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.mapper.DataSourcePOJODao;
import com.bmadmin.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by SEELE on 2017/12/17.
 */
@Service
@Transactional
public class DataSourceServiceImpl implements DataSourceService {
    @Autowired
    DataSourcePOJODao dataSourcePOJODao;

    @Override
    public LayUiDataTablePOJO getDataSourceList(DataSourcePOJO dataSourcePOJO) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();
        List<DataSourcePOJO> dataSourcePOJOS = dataSourcePOJODao.find(dataSourcePOJO);

        result.setCode(0);
        result.setMsg("查询成功");
        result.setCount(dataSourcePOJOS.size());
        result.setData(dataSourcePOJOS);

        return result;
    }

    @Override
    public NormalResult deleteDataSource(DataSourcePOJO dataSourcePOJO) {
        NormalResult result = new NormalResult();

        int sqlResult = dataSourcePOJODao.deleteByT_id(dataSourcePOJO.getT_id());
        if (sqlResult == 1) {
            result.setCode("0000");
            result.setMsg("删除成功");
        } else {
            result.setCode("9999");
            result.setMsg("删除异常，请联系系统管理员");
        }

        return result;
    }

    @Override
    public NormalResult updateDataSource(DataSourcePOJO dataSourcePOJO) {
        NormalResult result = new NormalResult();

        DataSourcePOJO dataSourcePOJOForQueryName = new DataSourcePOJO();
        dataSourcePOJOForQueryName.setT_name(dataSourcePOJO.getT_name());

        List<DataSourcePOJO> dataSourcePOJOS = dataSourcePOJODao.find(dataSourcePOJOForQueryName);
        if(dataSourcePOJOS.size()>0){
            if(dataSourcePOJOS.size()==1){
                if(!dataSourcePOJO.getT_id().equals(dataSourcePOJOS.get(0).getT_id())){
                    result.setCode("9999");
                    result.setMsg("数据源名称不得重复！");
                }

            }else{
                result.setCode("9999");
                result.setMsg("数据源名称不得重复！");
                return result;
            }
        }

        int sqlResult = dataSourcePOJODao.update(dataSourcePOJO);
        if (sqlResult == 1) {
            result.setCode("0000");
            result.setMsg("修改成功");
        } else {
            result.setCode("9999");
            result.setMsg("修改异常，请联系系统管理员");
        }


        return result;
    }

    @Override
    public NormalResult insertDataSource(DataSourcePOJO dataSourcePOJO) {
        NormalResult result = new NormalResult();

        DataSourcePOJO dataSourcePOJOForQueryName = new DataSourcePOJO();
        dataSourcePOJOForQueryName.setT_name(dataSourcePOJO.getT_name());

        List<DataSourcePOJO> dataSourcePOJOS = dataSourcePOJODao.find(dataSourcePOJOForQueryName);
        if (dataSourcePOJOS.size() > 0) {
            result.setCode("9999");
            result.setMsg("数据源名称不得重复！");
            return result;
        }

        int sqlResult = dataSourcePOJODao.insertSelective(dataSourcePOJO);
        if (sqlResult == 1) {
            result.setCode("0000");
            result.setMsg("新增成功");
        } else {
            result.setCode("9999");
            result.setMsg("新增异常，请联系系统管理员");
        }

        return result;
    }
}
